<?php

require_once ("./../libs/GIFEncoder.class.php");
require_once ("./../libs/db.class.php");
$db = new DB;

$width = 150;
$height = 100;
$countChars = 4;
$countMoveFrames = 1;
$startX = 15;
$startY = 25;
$margin = 8;
$fontSize = 24;
$angleDifference = 15;
$yDifference = 20;
$anglePlusMinus = 1.5;
$timing = 15;

$grayShader = 255 - rand() % 40;
$color = imagecolorallocate(imagecreatetruecolor(1, 1), $grayShader, $grayShader, $grayShader);

$backgroundsFolder = scandir("./backgrounds");
$backgrounds = array();
foreach($backgroundsFolder as $backIndex => $backValue) {
    if(mb_substr($backValue, mb_strpos($backValue, ".") + 1) == "png") {
        $backgrounds[] = $backValue;
    }
}

$background = imagecreatefrompng("./backgrounds/" . $backgrounds[rand() % count($backgrounds)]);

$randomY = array();

$fontsFolder = scandir("./fonts");
$fonts = array();
foreach($fontsFolder as $fontIndex => $fontValue) {
    if(mb_substr($fontValue, mb_strpos($fontValue, ".") + 1) == "ttf") {
        $fonts[] = $fontValue;
    }
}

$font = array();
$ch = array();
$angle = array();
$rotate = array();
$str = "";
for ($i = 0; $i < $countChars; $i++) {
    $randomY[] = $fontSize + rand() % $yDifference; 
    $font[] = "./fonts/" . $fonts[rand() % count($fonts)];
    $ch[] = chr(65 + rand() % 26);
    $str .= $ch[$i];
    $angle[$i] = (int) ($angleDifference * sin(rand()));
    if (sin(rand()) > 0) {
        $rotate[$i] = true;
    } else {
        $rotate[$i] = false;
    }
}

//<пишем текущую капчу с заменой>

$ip = $_SERVER["REMOTE_ADDR"];
if ($_SERVER["HTTP_X_FORWARDED_FOR"] != "") {
    $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
}

$db->dbq("
            Select
                Count(*)
            From
                capchas
            Where
                ip = \"" . $ip . "\"
        ");

$arr = $db->row();
if ($arr[0] == "0") {
    $db->dbq("
                Insert
                Into
                    capchas
                Values
                    (
                        0,
                        \"" . $ip . "\",
                        \"" . $str . "\",
                        Now()
                    )
            ");
} else {
    $db->dbq("
                Update
                    capchas
                Set
                    capchatext = \"" . $str . "\",
                    capchatake = Now()
                Where
                    ip = \"" . $ip . "\"
            ");
}

//<пишем текущую капчу с заменой>

$image = array();
for ($i = 0; $i < ($countMoveFrames * 2 + 2); $i++) {
    $image[] = imagecreatetruecolor($width, $height);
    imagecolorallocatealpha($image[$i], 0, 0, 0, 127);
    imagecopy($image[$i], $background, 0, 0, 0, 0, $width, $height);
}

for ($j = 0; $j < $countChars; $j++) {
    imagettftext($image[0], $fontSize, $angle[$j], $startX + ($fontSize + $margin) * $j, $startY + $randomY[$j], $color, $font[$j], $ch[$j]);
}

for ($i = 1; $i < $countMoveFrames + 2; $i++) {
    for ($j = 0; $j < $countChars; $j++) {
        if ($rotate[$j]) {
            $curAngle = $angle[$j] + $anglePlusMinus * $i;
        } else {
            $curAngle = $angle[$j] - $anglePlusMinus * $i;
        }
        imagettftext($image[$i], $fontSize, $curAngle, $startX + ($fontSize + $margin) * $j, $startY + $randomY[$j], $color, $font[$j], $ch[$j]);
    }
}

for ($i = 2 + $countMoveFrames; $i < ($countMoveFrames * 2 + 2); $i++) {
    for ($j = 0; $j < $countChars; $j++) {
        if ($rotate[$j]) {
            $curAngle = $angle[$j] + $anglePlusMinus * ($countMoveFrames * 2 + 2 - $i);
        } else {
            $curAngle = $angle[$j] - $anglePlusMinus * ($countMoveFrames * 2 + 2 - $i);
        }
        imagettftext($image[$i], $fontSize, $curAngle, $startX + ($fontSize + $margin) * $j, $startY + $randomY[$j], $color, $font[$j], $ch[$j]);
    }
}

$frames = array();
$framesTiming = array();
for ($i = 0; $i < ($countMoveFrames * 2 + 2); $i++) {
    ob_start();
    call_user_func_array("imagegif", array($image[$i]));
    $frames[] = ob_get_contents();
    ob_end_clean();
    $framesTiming[] = $timing;
}

$gif = new GIFEncoder($frames, $framesTiming, 0, 0, 0, 0, 0, "bin");

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
header("Content-type: image/gif");
echo $gif->GetAnimation();

for ($i = 0; $i < ($countMoveFrames * 2 + 2); $i++) {
    imagedestroy($image[$i]);
}
imagedestroy($background);
?>